package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Dept;
import org.example.pojo.Result;
import org.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.logging.Logger;
// Slf4j 导入日志相关的包
@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    // 查询部门列表
//    @RequestMapping(value = "/depts", method = RequestMethod.GET)
    @GetMapping("/depts")
    public Result list() {
        log.info("查询部门列表");

        // 调用Service查询部门列表

        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }

    /**
     * 删除某个部门
     */
    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id删除部门，id：{}", id);

        deptService.delete(id);

        return Result.success();
    }

    /**
     * 新增部门
     */
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept) {
        log.info("新增部门，参数：{}", dept);
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(null);
        deptService.add(dept);

        return Result.success();
    }
}
